package com.zxht.wuchechenyun.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

import com.zxht.wuchechenyun.R;

import java.util.HashMap;
import java.util.Map;


public abstract class BaseActivity extends FragmentActivity implements
		OnClickListener {
	private Map<Integer, Runnable> allowablePermissionRunnables = new HashMap<>();
	private Map<Integer, Runnable> disallowablePermissionRunnables = new HashMap<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		super.onCreate(savedInstanceState);
		setContentView(getLayoutResID());
		
		//初始化空间
		initView();

		//初始化监听
		initListener();
//		registerCommonBtn();


	}

	public abstract int getLayoutResID();

	public abstract void initView();

	/**
	 * 初始化监听：点击监听、设置适配器、设置条目点击监听
	 */
	public abstract void initListener();

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ll_go_back:
				finish();
				break;
		default:
			onInnerClick(v);
			break;

		}
	}
//	/**
//	 * 注册相同逻辑 返回按钮
//	 */
//	private void registerCommonBtn() {
//		View backView=findViewById(R.id.ll_go_back);
//
//		if (backView!=null) {
//			backView.setOnClickListener(this);
//		}
//	}

	public void onInnerClick(View v) {

	}

	public void toast(String msg) {
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 请求权限
	 * @param id 请求授权的id 唯一标识即可
	 * @param permission 请求的权限
	 * @param allowableRunnable 同意授权后的操作
	 * @param disallowableRunnable 禁止权限后的操作
	 */
	protected void requestPermission(int id, String permission, Runnable allowableRunnable, Runnable disallowableRunnable) {
		if (allowableRunnable == null) {
			throw new IllegalArgumentException("allowableRunnable == null");
		}

		allowablePermissionRunnables.put(id, allowableRunnable);
		if (disallowableRunnable != null) {
			disallowablePermissionRunnables.put(id, disallowableRunnable);
		}

		//版本判断
		if (Build.VERSION.SDK_INT >= 23) {
			//减少是否拥有权限
			int checkCallPhonePermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
			if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
				//弹出对话框接收权限
				ActivityCompat.requestPermissions(this, new String[]{permission}, id);
				return;
			} else {
				allowableRunnable.run();
			}
		} else {
			allowableRunnable.run();
		}
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if(grantResults.length==0)return;
		if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
			Runnable allowRun = allowablePermissionRunnables.get(requestCode);
			allowRun.run();
		} else {
			Runnable disallowRun = disallowablePermissionRunnables.get(requestCode);
			disallowRun.run();
		}
	}

//	public static void outLogin(String resultCode, Activity activity){
//		if (resultCode.equals("502")){
//			SPUtils.put(activity, MySpResource.USER_NAME,"");//制空，省略首页请求登录状态步骤
//			Intent intent=new Intent(activity,LoginActivity.class);
//			activity.startActivity(intent);
//			activity.finish();
//		}
//	}
//	public static boolean isApplicationBroughtToBackground(final Context context) {
//		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//		List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
//		if (!tasks.isEmpty()) {
//			ComponentName topActivity = tasks.get(0).topActivity;
//			if (!topActivity.getPackageName().equals(context.getPackageName())) {
//				return true;
//			}
//		}
//		return false;
//	}
}
